package 打牢基础30天.day02.my;

import 打牢基础30天.util.ArrUtil;

/**
 * @Author yaya
 * @Create 2024/6/13 10:24
 * Description:快排
 */
public class Code06_QuickSort {

    public static void quickSort(int arr[]) {
        if (arr == null || arr.length < 2) {
            return;
        }

        quickSort(arr, 0, arr.length - 1);
    }

    private static void quickSort(int[] arr, int left, int right) {
        if (left < right) {
            // 交换left -- right 中随机一个数到有节点
            swap(arr, right, left + (int) (Math.random() * right - left + 1));
            int[] p = pattion(arr, left, right);
            quickSort(arr, left, p[0] - 1);
            quickSort(arr, p[1] + 1, right);
        }

    }

    /**
     * 使用荷兰国旗问题，将传入的数组分成小中大三块，使用递归使得局部到整体完成排序
     *
     * @param arr
     * @param left
     * @param right
     * @return
     */
    private static int[] pattion(int[] arr, int left, int right) {
        int less = left - 1;
        int more = right;
        // 用最右边的值做基准值
        while (left < more) {
            if (arr[left] < arr[right]) {
                swap(arr, ++less, left++);
            } else if (arr[left] > arr[right]) {
                swap(arr, --more, left);
            } else {
                left++;
            }
        }
        // 结束后将基准值放入中间
        swap(arr, more, right);
        return new int[]{less + 1, more};
    }

    public static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }


    public static void quickSort2(int[] arr) {
        if (arr == null && arr.length < 2) {
            return;
        }
        quickSort2(arr, 0, arr.length - 1);
    }

    private static void quickSort2(int[] arr, int left, int right) {
        if (left >= right){
            return;
        }
        int pivotIndex = partition(arr, left, right);
        quickSort2(arr, left, pivotIndex - 1);
        quickSort2(arr, pivotIndex + 1, right);
    }

    private static int partition(int[] arr, int left, int right) {
        int pivot = arr[left];
        int low = left + 1 ;
        int high = right;

        while (low <= high){
            while (low <= high && arr[high] >= pivot){
                    high--;
            }
            while (low <= high && arr[low] <= pivot ){
                low++;
            }
            if (low < high){
                swap(arr, low, high);
            }
        }
        swap(arr, left, high);
        return high;
    }

    // for test
    public static void main(String[] args) {
        int testTime = 500000;
        int maxSize = 100;
        int maxValue = 100;
        boolean succeed = true;
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < testTime; i++) {
            int[] arr1 = ArrUtil.generateRandomArray(maxSize, maxValue);
            int[] arr2 = ArrUtil.copyArray(arr1);
            quickSort2(arr1);
            ArrUtil.comparator(arr2);
            if (!ArrUtil.isEqual(arr1, arr2)) {
                succeed = false;
                break;
            }
        }
        long endTime = System.currentTimeMillis();
        System.out.println("costTime:" + (endTime - startTime) + "ms");

        System.out.println(succeed ? "Nice!" : "Fucking fucked!");

        int[] arr = ArrUtil.generateRandomArray(maxSize, maxValue);
        ArrUtil.printArray(arr);
        quickSort(arr);
        ArrUtil.printArray(arr);
    }
}
